package task.manager;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TaskViewActivity extends Activity{
	
	private TaskFacade taskf;
	private TaskList tasks;
	private String[] taskNames;
	private Button addTaskBtn;
	private Button backBtn;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("TaskViewActivity onCreate sucessfully called w/ super");
        setContentView(R.layout.taskviewactivity);
        
        taskf=TaskFacade.getInstance();
        System.out.println("here1");
		tasks=taskf.getTaskList();
		//Test Code***********
		if(tasks == null){
			tasks = new TaskList();
			tasks.addTask(new Task("Test", 0, 0, 0, new Integer(0), new Integer(0)));
		}
		//TestCode************
		taskNames = tasks.toArray();
		
		addTaskBtn = (Button) findViewById(R.id.addTask);
		backBtn = (Button) findViewById(R.id.backBtn);
		
		final ListView lv = (ListView) findViewById(R.id.list_view1);
		lv.setTextFilterEnabled(true);
		
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
				taskNames));

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//gets the label of the clicked item
				String selectedTask = (String) lv.getItemAtPosition(position);
				System.out.println(selectedTask);
				taskf.setActiveTask(selectedTask);
				System.out.println("meow");

				
				Intent taskScreen = new Intent(getApplicationContext(),
						ViewTaskScreen.class);
				//ViewTaskScreen doesn't currently exist. We need a screen that shows the task's info.
				System.out.println("Intent finished");
				startActivity(taskScreen);
				System.out.println("startActivity finished");
				finish();
				

				// When clicked, show a go to the category's tasks

			}
		});
		
		/*System.out.println("" + tasks);
		String[] titles = tasks.toArray();
		
		if(titles != null){
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, titles);*/
		
		
		System.out.println("here4");
		
		addTaskBtn.setOnClickListener(new OnClickListener() {
			/**
			 * Response to clicking Add new category.
			 */
			public void onClick(View v) {
				// goto category creation screen
				Intent nextScreen = new Intent(getApplicationContext(),
						AddTaskScreen.class);
				startActivity(nextScreen);
				finish();
			} // End of onClick method.
		}); // End of setOnClickListener.
		
		System.out.println("here5");
		backBtn.setOnClickListener(new OnClickListener() {
			/**
			 * Response to clicking Add new category.
			 */
			public void onClick(View v) {
				// goto category creation screen
				Intent nextScreen = new Intent(getApplicationContext(),
						CategoryViewActivity.class);
				startActivity(nextScreen);
				finish();
			} // End of onClick method.
		}); // End of setOnClickListener.
	}

}